$(function() {

    var listElems = new Object();
    var listElems = {}; // or, more conveniently:
//    listElems[45] = {name: 'John Doe'};

    var clickElem = $('.tags>li>a');
    var mask_obj = '#results';
    var delayTime = 50;
    var dropScrollTime = 1300;

    var results_container = "#results";
    var item_container_id = "#paging_cont";
    var items_per_page = 5;

    var selElem = function(elem) {
        $(elem).parent().parent().find('li').removeClass('active');
        $(elem).parent().addClass('active');
    }

    var filterExc = function() {

        var cur_ID = $(this).attr('data-id');
        selElem(this);
        doFilter(cur_ID, false);

    };

    var resetExc = function() {
        $(this).parent().parent().find('li').removeClass('active');
        doFilter(-1, false);
    };

    Object.size = function(obj) {
        var size = 0, key;
        for (key in obj) {
            if (obj.hasOwnProperty(key))
                size++;
        }
        return size;
    };

    clickElem.on('click', filterExc);

    var i = 999;  // due to unique asoociative array keys
    $("ul#paging_cont>li").each(function(index, value) {

        var ID = String($(this).attr('data-id'));
        listElems[i + ',' + ID] = value;
        i++;
    });

    $('a.resetExc').on('click', resetExc);

    API.Data.IS_FILTERING = false;

    var doFilter = function(cur_ID, effectOnly) {

        API.Data.IS_FILTERING = true;

        $(mask_obj).mask(API.Data.lang['filter_filtering'] + "...");
        var rand_num = delayTime + Math.floor(Math.random() * 501);

        $.scrollTo('.form-holder', dropScrollTime, {onAfter: function() {

                window.setTimeout(function() {

                    if (!effectOnly) {

                        $("ul#paging_cont>li").remove();

                        if (cur_ID != -1) {
                            $.each(listElems, function(index, value) {

                                var myArr = index.split(",");

                                if ($.inArray(cur_ID, myArr) != -1)
                                    $("ul#paging_cont").append(value);
                            });
                        } else {
                            $.each(listElems, function(index, value) {
                                $("ul#paging_cont").append(value);
                            });
                        }

                        if ($("ul#paging_cont>li").length == 0) {
                            $("ul#paging_cont").append('<li class="no-value">No results...</li>');
                        }

                        initPager();

                    }

                    $(mask_obj).unmask();

                    API.Data.IS_FILTERING = false;

                }, rand_num);

            }
        });

    };

    $.fn.preBind = function(type, data, fn) {
        this.each(function() {
            var $this = $(this);

            $this.bind(type, data, fn);

            var currentBindings = $._data(this, "events")[type];
            if ($.isArray(currentBindings)) {
                currentBindings.unshift(currentBindings.pop());
            }
        });
        return this;
    };

    var initPager = function() {

        $(results_container).pajinate({
            items_per_page: items_per_page,
            item_container_id: item_container_id,
            wrap_around: false,
            nav_label_first: '<<<',
            nav_label_last: '>>>',
            nav_label_prev: '<<',
            nav_label_next: '>>',
            num_page_links_to_display: 7,
            show_first_last: false
        });

        // On each reload reset the pagination on click handler
        $(".page_link").preBind("click", function() {

            if (API.Data.IS_FILTERING === true || $(this).hasClass('active_page')) {
                return false;
            }
            doFilter(false, true);
        });
        $(".previous_link").preBind("click", function() {
            if (API.Data.IS_FILTERING === true || $(this).hasClass('active_page')) {
                return false;
            }
            doFilter(false, true);
        });
        $(".next_link").preBind("click", function() {
            if (API.Data.IS_FILTERING === true || $(this).hasClass('active_page')) {
                return false;
            }
            doFilter(false, true);
        });

    };

    initPager();  

});